package com.coocaa.ops.admin.v1.auth.sys.biz.impl;

import com.coocaa.ops.admin.v1.auth.sys.biz.ISysLogLoginBiz;
import com.coocaa.ops.admin.v1.auth.sys.mapper.ISysLogLoginMapper;
import com.coocaa.ops.common.core.base.BaseBizImpl;
import com.coocaa.ops.common.core.biz.auth.entity.SysLogLoginEntity;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.tool.date.DateUtil;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author bijiahao
 * @date : 2019/3/18.
 * @description
 */
@Service
public class SysLogLoginBizImpl extends BaseBizImpl<ISysLogLoginMapper, SysLogLoginEntity> implements ISysLogLoginBiz {

    @Override
    public SysLogLoginEntity add(SysLogLoginEntity entity) {
        getMapper().insert(entity);
        return entity;
    }

    /**
     * 保存登录日志
     *
     * @param loginIp
     * @param user
     * @param userAgent
     */
    @Override
    public void saveSysLogLogin(String loginIp, SysUserEntity user, String userAgent) {
        Date loginTime = DateUtil.getCurDate();
        SysLogLoginEntity login = new SysLogLoginEntity();
        if (user != null) {
            //修复退出空指针异常导致的无法跳转
            login.setUserName(user.getLoginName());
            login.setUserId(user.getId());
        }
        login.setLoginTime(loginTime);
        login.setLoginIp(loginIp);
        login.setLoginType(1);
        login.setUserAgent(userAgent);
        login.setRemark("Log out");
        add(login);
    }
}
